package com.ying.workqueues;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * ClassName: WorkQueueConsumer01
 * Package: com.ying.workqueues
 * Description:
 *
 * @Author 刘英杰
 * @Create 2025/3/23 19:56
 * @Version 1.0
 */
public class WorkQueueConsumer01 {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.160.134");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("myHV");
        connectionFactory.setUsername("testUser");
        connectionFactory.setPassword("123456");

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        channel.queueDeclare("workQueue", true, false, false, null);

        channel.basicConsume("workQueue", true, new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println(new String(body));
            }
        });
    }
}
